package edu.sctu.zhuyingzhi.month201505.day20150525.getNetMsgTest;

/**
 * Created by user13 on 2015/5/25.
 */
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetDataByURL {
    public static String cc(String url) {
        StringBuffer temp = new StringBuffer();
        try {

            URL url1 = new URL(url);
            HttpURLConnection uc = (HttpURLConnection)url1.
                    openConnection();
            uc.setConnectTimeout(10000);
            uc.setDoOutput(true);
            uc.setRequestMethod("POST");
            uc.setUseCaches(false);
            DataOutputStream out = new DataOutputStream(uc.getOutputStream());

            // 要传的参数

            // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面

            out.flush();
            out.close();
            InputStream in = new BufferedInputStream(uc.getInputStream());
            Reader rd = new InputStreamReader(in, "utf-8");
            System.out.println(uc.getContentEncoding());
            System.out.println(uc.getContentType());
            System.out.println(uc.getContentLength());
            System.out.println(uc.getContentLengthLong());
            System.out.println(uc.getContent());
            int c = 0;
            while ((c = rd.read()) != -1) {
                temp.append((char) c);
            }
            System.out.println(temp.toString());
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp.toString();
    }

  
    public static void main(String[] a){
        GetDataByURL.cc("http://www.baidu.com");
    }
}
